package com.nti56.springboot.controller;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nti56.springboot.model.AjaxResult;
import com.nti56.springboot.model.DataList;
import com.nti56.springboot.model.PageResponse;
import com.nti56.springboot.model.Role;
import com.nti56.springboot.service.RoleService;
import com.nti56.springboot.utils.Constant;



@RequestMapping("role")
@Controller
public class RoleController extends BaseController{
    
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

	
	@Autowired
    private RoleService roleService;
	
	@RequestMapping("/toRoleList")
	@RequiresPermissions("Role:toRoleList")
    public String toRoleList(Model model){		
		model.addAttribute("roleType", Constant.SYS_ROLE_TYPE_OPERATE);
        return "system/roleList";
    }
	
	@RequestMapping("/toDataRoleList")
	@RequiresPermissions("Role:toDataRoleList")
    public String toDataRoleList(Model model){
		model.addAttribute("roleType", Constant.SYS_ROLE_TYPE_DATA_AUTH);
        return "system/dataRoleList";
    }
	
	@RequestMapping("/toAuthSetting")
    public String toAuthSetting(HttpServletRequest request, Model model){
		String roleId = (String) request.getParameter("roleId");
		model.addAttribute("roleId", roleId);
        return "system/authSetting";
    }
	
	
	@RequestMapping("/toDataAuthSetting")
    public String toDataAuthSetting(HttpServletRequest request, Model model){
		String roleId = (String) request.getParameter("roleId");
		model.addAttribute("roleId", roleId);
        return "system/dataAuthSetting";
    }
	
	
	@RequestMapping("/toRoleAdd")
    public String toRoleAdd(HttpServletRequest request, Model model){
		String roleType = (String) request.getParameter("roleType");
		model.addAttribute("roleType", roleType);
        return "system/roleAdd";
    }
	
	@RequestMapping("toRoleUpdate")
	public String toRoleUpdate(HttpServletRequest request, Model model){
		String roleId = (String) request.getParameter("roleId");
		String roleType = (String) request.getParameter("roleType");
		Role role = roleService.getRoleByRoleId(roleId);
		model.addAttribute("roleId", roleId);
		model.addAttribute("role", role);
		model.addAttribute("roleType", roleType);
        return "system/roleAdd";
	}
	
	@RequestMapping("/addRole")
    @ResponseBody
    public AjaxResult addRole(HttpServletRequest request,Role role){    	
    	try {
    		String roleId = (String) request.getParameter("roleId");
			if(roleId == null || "".equals(roleId)) {
				roleService.addRole(role);
				return new AjaxResult(true,AjaxResult.SAVE_OK);
			}else {
				roleService.updateRole(role);
				return new AjaxResult(true,AjaxResult.UPDATE_OK);
			}			
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.SAVE_FAIL);
		}
    }
	
	
	@RequestMapping("/listRoles")
    @ResponseBody
    public PageResponse listRoles(HttpServletRequest request){
    	Map map = this.getParamMap(request);
    	this.startPage(request);
        List<Role> list = roleService.getRoleList(map);
        return this.getPageResponse(list);
    }
	
	@RequestMapping("/getAllRoles")
    @ResponseBody
    public List<DataList> getAllRoles(HttpServletRequest request){
		Map map = this.getParamMap(request);
        List<Role> list = roleService.getRoleList(map);
        List<DataList> dataLists = new ArrayList<DataList>();
        DataList dl = null;
        for(Role r : list){
        	dl = new DataList();
        	dl.setValue(r.getRoleId());
        	dl.setText(r.getRoleName());
        	dataLists.add(dl);
        }
        return dataLists;
    }
	
	@RequestMapping("/deleteRole")
    @ResponseBody
    public AjaxResult deleteRole(HttpServletRequest request){
    	try {
    		Map map = this.getParamMap(request);
        	String ids = (String) map.get("ids");
    		List<String> list = Arrays.asList(ids.split(","));
    		for(String roleId : list){
    			roleService.deleteRole(roleId);
    		}
			return new AjaxResult(true,AjaxResult.DLELETE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			return new AjaxResult(false,e.getMessage(),AjaxResult.DLELETE_FAIL);
		}
    }
}